package com.supreme.commontools;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collections;

public class FilePathUtils {

    public static final String userDirPath = System.getProperty("user.dir");

    public static Path getPath(String path) {
        return Paths.get(userDirPath, path);
    }

    public static void createFile(Path path) {
        try {
            if (!Files.exists(path)) {
                Files.createFile(path);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeFile(Path path, Iterable<? extends CharSequence> lines) {
        try {
            Files.write(path, lines);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeFile(Path path, String line) {
        FilePathUtils.writeFile(path, Collections.singleton(line));
    }

    public static void appendWriteFile(Path path, Iterable<? extends CharSequence> lines) {
        try {
            Files.write(path, lines, StandardOpenOption.APPEND);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void appendWriteFile(Path path, String line) {
        FilePathUtils.appendWriteFile(path, Collections.singleton(line));
    }
}
